<template>
  <div class="register-container">
    <!-- 左侧装饰区域 -->
    <div class="register-left">
      <div class="decoration-content">
        <div class="icon-group">
          <el-icon :size="100" color="#FFFFFF">
            <Management />
          </el-icon>
        </div>
        <h1 class="welcome-title">注册账号</h1>
        <h2 class="system-name">火电知识图谱构建平台</h2>
        <p class="system-desc">
          基于大语言模型和提示学习<br/>
          智能抽取火电领域专业知识
        </p>
      </div>
    </div>

    <!-- 右侧表单区域 -->
    <div class="register-right">
      <div class="form-container">
        <div class="form-header">
          <el-icon :size="48" color="#409EFF">
            <UserFilled />
          </el-icon>
          <h2 class="form-title">注册账号</h2>
        </div>

        <el-form 
          :model="registerForm" 
          :rules="rules" 
          ref="registerFormRef"
          size="large"
        >
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入用户名"
              clearable
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请确认密码"
              show-password
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-button 
            type="primary" 
            :loading="loading"
            @click="handleRegister"
            class="submit-button"
            size="large"
          >
            注册
          </el-button>
        </el-form>

        <div class="footer-links">
          <span class="hint-text">已有账号？</span>
          <el-link type="primary" @click="$router.push('/login')">
            立即登录
          </el-link>
        </div>

        <div class="user-agreement">
          注册即表示您同意我们的用户协议和隐私政策
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store'
import { ElMessage } from 'element-plus'
import { User, UserFilled, Lock, Management } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

const registerFormRef = ref(null)
const loading = ref(false)

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})

const validatePassword = (rule, value, callback) => {
  if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validatePassword, trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid) => {
    if (!valid) return
    
    loading.value = true
    try {
      const res = await userStore.register(
        registerForm.username,
        registerForm.password
      )
      if (res.success) {
        ElMessage.success('注册成功，请登录')
        router.push('/login')
      } else {
        ElMessage.error(res.message || '注册失败')
      }
    } catch (error) {
      ElMessage.error(error.response?.data?.message || '注册失败，请检查网络连接')
    } finally {
      loading.value = false
    }
  })
}
</script>

<style scoped>
.register-container {
  display: flex;
  width: 100vw;
  height: 100vh;
  background: #FFFFFF;
  overflow: hidden;
}

/* 左侧装饰区域 */
.register-left {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  background: #1463b3;
  position: relative;
}

.decoration-content {
  position: relative;
  z-index: 1;
  text-align: center;
  color: white;
}

.icon-group {
  margin-bottom: 48px;
}

.welcome-title {
  font-size: 32px;
  font-weight: 300;
  margin: 0 0 16px 0;
  opacity: 0.95;
}

.system-name {
  font-size: 48px;
  font-weight: 600;
  margin: 0 0 24px 0;
  letter-spacing: 2px;
}

.system-desc {
  font-size: 18px;
  line-height: 1.8;
  opacity: 0.9;
  font-weight: 300;
}

/* 右侧表单区域 */
.register-right {
  width: 480px;
  background: var(--bg-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: -4px 0 24px rgba(0, 0, 0, 0.1);
}

.form-container {
  width: 360px;
  padding: 40px 0;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header .el-icon {
  margin-bottom: 16px;
}

.form-title {
  font-size: 28px;
  font-weight: 500;
  color: var(--text-primary);
  margin: 0;
}

.el-form {
  margin-top: 32px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 0 0 1px var(--border-light) inset;
  transition: all 0.3s;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px var(--border-base) inset;
}

:deep(.el-input.is-focus .el-input__wrapper) {
  box-shadow: 0 0 0 1px var(--primary-blue) inset;
}

.submit-button {
  width: 100%;
  margin-top: 8px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  height: 48px;
}

.footer-links {
  text-align: center;
  margin-top: 24px;
  font-size: 14px;
}

.hint-text {
  color: var(--text-secondary);
  margin-right: 8px;
}

.user-agreement {
  text-align: center;
  margin-top: 20px;
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.6;
  padding: 0 20px;
}

/* 响应式 */
@media (max-width: 1024px) {
  .register-left {
    display: none;
  }
  
  .register-right {
    width: 100%;
  }
}
</style>
